var app = getApp()
const Api= require('../../services/api');
const userService = require('../../services/userService');
import Dialog from '../../vant/dialog/dialog';
import Toast from '../../vant/toast/toast';
Page({
  data: {
      imgwg:'',
      imgxb:'',
      imgzd:'',
      imgxdzm:'',
      imgxdfm:'',
      imgxhgy:'',
      imgxhcb:'',
      imggmdj:'',
      imgothers:[],
      assets:app.assets,
      assetsByEnv:app.assetsByEnv,
      steps: [
          {
              text: '上传图片',
              desc: ''
          },
          {
              text: 'AI智能初审',
              desc: ''
          },
          {
              text: '人工复审',
              desc: ''
          }
      ],
      active:0
  },

    onShareAppMessage: function () {
        return {
            imageUrl:this.data.assetsByEnv+'/tanchuang1.png',
            title: '鞋库鉴定',
            path: '/pages/home/home',
            success: function(res) {
            },
            fail: function(res) {
            }
        }
    },
  onShow: function(){

  },
  upload:function(e) {
    var field = e.currentTarget.dataset.field;
    var that = this;
    var user = wx.getStorageSync('user');
    wx.chooseImage({
        count: 1,
        fail:function(res){
            //非用户取消上传错误
            if(res.errMsg != 'chooseImage:fail cancel') {
                wx.showToast({title:'选择图片失败'+res.errMsg,icon:'none'});
            }
        },
        success: function(res) {
            Toast.loading({
                mask: true,
                message: '图片上传中...',
                duration:0
            });
            var tempFilePaths = res.tempFilePaths;
            var formData = {};
            // formData.openid=user.openid;
            // console.error(formData);
            var uploadTask = wx.uploadFile({
                url: Api.upload, //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                header: {
                    'content-type':'multipart/form-data'
                },
                formData:formData,
                name: 'file',
                success: function(res){
                    Toast.clear();
                    if (res.statusCode==200) {
                        var data = res.data;
                        data = JSON.parse(data);
                        if (data.result) {
                            var name = data.obj.name;
                            // console.error(showUrl);
                            if (field == 'imgothers') {
                                that.data.imgothers.push(Api.download + name);
                                that.setData({
                                    imgothers:that.data.imgothers
                                })
                            } else {
                                that.setData({
                                    [field]:Api.download + name
                                })
                            }

                        }else{
                        }
                    }else{
                        wx.showToast({
                            title: '接口异常'+res.statusCode,
                            icon:'none'
                        });
                    }
                },
                fail:function(res){
                    wx.showToast({title:'图片上传失败'+res.errMsg,icon:'none'});
                }
            })
            uploadTask.onProgressUpdate((res) => {
                that.setData({
                    percents: res.progress
                });
                if(res.progress == 100) {
                    setTimeout(function(){
                        that.setData({
                            percents: 0
                        });
                    },1000);
                }
            })
        }
    });
  },
  goIns:function() {
    wx.navigateTo({
        url: '../appraIns/appraIns'
    })
  },
    addAppra:function(){
        var user = wx.getStorageSync('user');
        var openid = user.openid;
        if (!this.data.imgwg || !this.data.imgxb || !this.data.imgzd || !this.data.imgxdzm || !this.data.imgxdfm) {
            Dialog.alert({
                confirmButtonText:'确定',
                title: '提示',
                message: '外观、鞋标、中底、鞋底正面、鞋底反面必须提供，否则无法鉴定'
            }).then(() => {
                // on close
            });
            return;
        }

        var params = {
            customerId:openid,
            img1:this.data.imgwg,
            img2:this.data.imgxb,
            img3:this.data.imgzd,
            img4:this.data.imgxdzm,
            img5:this.data.imgxdfm,
            img6:this.data.imgxhgy,
            img7:this.data.imgxhcb,
            img8:this.data.imggmdj,
            otherimg:this.data.imgothers.join(',')
        };
        Toast.loading({
            mask: true,
            message: '鉴定提交中...',
            duration:0
        });
        userService.addAppra(params).then((data=>{
            if(data.result) {
                this.setData({
                    active:1
                });
            Toast.loading({
                mask: true,
                message: 'AI智能初审中',
                duration:0
            });
            var id =data.obj.id;
            setTimeout(function(){
                Toast.clear();
                wx.reLaunch({
                    url: '../appraDetail/appraDetail?id=' + id
                })
            },3000);
            }else{
                wx.showToast({title:data.errorMsg,icon:'none'});
            }
            }),(error=>{
                wx.showToast({title:'addAppra接口异常',icon:'none'});
        }));
    },
  onLoad: function (options) {
      wx.setNavigationBarTitle({
          title:'鞋库鉴定'
      })

  }
});
